1.简介

Lua 断点调试是指在 Lua 脚本执行过程中,可以像调试 C#、Java 那样,设置断点、单步执行、查看变量、调用栈等。
常用的断点调试工具有EmmyLua,Decode等,本文主要介绍EmmyLua在不同编辑器场景的使用

1762911577906.png300

EmmyLua

github组织主页:https://github.com/EmmyLua
官方文档:https://emmylua.github.io/zh_CN/
VSCode插件页面:https://marketplace.visualstudio.com/items?itemName=tangzx.emmylua
IDEA插件页面:https://plugins.jetbrains.com/plugin/9768-emmylua
Emmylua原理简介:https://blog.csdn.net/qq_33060405/article/details/147405129

2. VSCode 使用

2.1 安装

在线安装

VSCode市场的更新相对较快,可以直接搜索安装
VSCode→Extensions→搜索EmmyLua安装

访问 https://marketplace.visualstudio.com/items?itemName=tangzx.emmylua 插件网页安装

离线安装

github上的Realease发布比较慢,因此推荐用上面的在线安装方式
访问 https://github.com/EmmyLua/VSCode-EmmyLua/releases 下载 vsix 插件
VSCode->Extensions->...→Install from VSIX

1762911643892.png300

2.2 调试器设置

安装完EmmyLua之后,左侧菜单栏会出现Run and Debug栏

选中之后,默认没有配置文件,选择新增配置文件,搜索栏出现选项框,选择EmmyLua New Debugger

1762911655997.png300

修改对应配置,可以参考如下,这里加了三种配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "emmylua_new",
            "request": "launch",
            "name": "IDE Connect Debugger",
            "host": "localhost",
            "port": 9966,
            "ext": [
                ".lua",
            ],
            "ideConnectDebugger": true,
        },
        {
            "type": "emmylua_new",
            "request": "launch",
            "name": "Debugger Connect IDE",
            "host": "localhost",
            "port": 9966,
            "ext": [
                ".lua",
            ],
            "ideConnectDebugger": false,
        },
        {
            "type": "emmylua_attach",
            "request": "attach",
            "name": "Attach by process id",
            "pid": pid,
            "processName": ""
        },
    ]
}

2.3 开始调试

方式 IDE Connect Debugger Debugger Connect IDE Attach By Process id
简介 IDE去连接Debugger,先运行Editor或游戏,让Debugger开始监听本地对应端口,之后让IDE去连接对应端口来建立连接 Debugger主动去连接IDE,先打开IDE的调试,让Debugger主动去连接IDE所监听的端口来建立连接 采用Attach的方式进行附加
调试器 下拉选择对应的调试器
1762911901142.png300
通过任务管理器,修改pid配置
1762911936414.png300
-- IDE Connect Debugger
-- 在main.lua 开始位置插入如下代码
-- dll 路径需要替换为自己安装路径

package.cpath = package.cpath .. ";c:/Users/{UserName}/.vscode/extensions/tangzx.emmylua-0.9.31-win32-x64/debugger/emmy/windows/x64/?.dll"
local dbg = require("emmy_core")
dbg.tcpListen('localhost', 9966)

-- 可选的,勾选后会在此暂停,直至IDE连接
-- dbg.waitIDE()
-- Debugger Connect IDE
-- 在main.lua 开始位置插入如下代码
-- 对应路径要替换为安装路径

package.cpath = package.cpath .. ";c:/Users/{UserName}/.vscode/extensions/tangzx.emmylua-0.9.31-win32-x64/debugger/emmy/windows/x64/?.dll"
local dbg = require("emmy_core")
dbg.tcpConnect("localhost", 9966)

-- 直接用这个代码的话,在启动时候可能会出现如下阻断,原因是还没有成功连接上对应端口
-- 在我们实际开发过程中,不是都需要启动IDE的,因此,可以用xpcall对这部分进行包裹来保证游戏顺利启动

xpcall(function ()
package.cpath = package.cpath .. ";c:/Users/{UserName}/.vscode/extensions/tangzx.emmylua-0.9.31-win32-x64/debugger/emmy/windows/x64/?.dll"
local dbg = require("emmy_core")
dbg.tcpConnect("localhost", 9966)
end, function(err)

end)
-- Attach By Process id
-- 采用Attach方式,不需要代码注入

2.4 断点方式

点击行号左侧(或者选中命令行 F9),右键Editor Breakpoint可以设置条件断点
代码中添加 dbg.breakHere() 增加断点

3.IDEA 使用

3.1 安装

IDEA->File->Setting

1762913415511.png300

JetBrain市场直接安装

Settings->Plugins->搜索emmyLua,会显示当前ide所支持的版本

离线安装

前往https://github.com/EmmyLua/IntelliJ-EmmyLua/releases下载对应版本插件Settings->Plugins->中央小齿轮→Install Plugins from Disk
选中对应目录本地文件安装

1762913459548.png300

3.2 调试配置

Project中将对应路径设置为SourceRoot

1762913503489.png300

调试准备
Run->EditorConfigurations

1762913537072.png300

Idea安装默认左侧应该会有EmmyDebugger(New)的一个调试器
如果没有的话可以点击左上角的小加号

微信图片_20251112104644_2222_5.png300

3.3 开始调试

该方式通过注入的方式,有两种连接方式 Connection

Attach的方式,在IDEA高版本中独立出来,作为单独一个插件EmmyLua Attach Debugger,已不再维护,IDEA 2023.3之后的版本无法正常安装(修改插件版本强制安装之后,IDEA不能适配所在行)

最新的消息在官方q群里公布,得等 EmmyLua2和EmmyLua2 Attach Debugger 更新

1762913589960.png300

方式 IDE connect Debugger Debugger connect IDE
简介 IDE去连接Debugger,先运行Editor或游戏,让Debugger开始监听本地对应端口,之后让IDE去连接对应端口来建立连接 Debugger主动去连接IDE,先打开IDE的调试,让Debugger主动去连接IDE所监听的端口来建立连接
调试器图示 Run→EditorConfigurations
1762913767588.png300
Run→EditorConfigurations
1762913778943.png300
UnityEditor
-- 在main.lua 开始位置插入如下代码
-- 对应路径要替换为安装路径
-- IDEA的代码可以在Debugger配置界面查看

package.cpath = package.cpath .. ';C:/Users/{UserName}/AppData/Roaming/JetBrains/IdeaIC2023.1/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
local dbg = require('emmy_core')
dbg.tcpListen('localhost', 9966)

// 可选的,添加上之后,调试器会在此停止,直到IDE连接成功才会再执行
-- dbg.waitIDE()
-- 在main.lua 开始位置插入如下代码
-- 对应路径要替换为安装路径
-- IDEA的代码可以在Debugger配置界面查看

package.cpath = package.cpath .. ';C:/Users/{UserName}/AppData/Roaming/JetBrains/IdeaIC2023.1/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

-- 在我们实际开发过程中,不是都需要启动IDE的,因此,可以用xpcall对这部分进行包裹来保证游戏顺利启动

xpcall(function ()
    package.cpath = package.cpath .. ';C:/Users/{UserName}/AppData/Roaming/JetBrains/IdeaIC2023.1/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
    local dbg = require('emmy_core')
    dbg.tcpConnect('localhost', 9966)
end, function(err)
   
end)

3.4 断点方式

点击对应区域设置断点(或者选中命令行 ctrl + F8),右键Editor可以设置条件断点
Ctrl + Shift + F8 查看所有断点
代码中添加 dbg.breakHere() 增加断点

4.个人使用心得

对于程序来说,我们项目很多都可以采用patch或者添加日志的的方式来查看数据
但是在开发过程中,需要一定的额外操作才能查看信息,以及比较难处理时序相关的问题
这个时候断点调试使用上就更为方便

笔者个人在实际的开发过程中,习惯使用 Debugger connect IDE 的方式来进行调试
优点在于IDE只需启用一次调试器,后续每次运行都会自动连接

5.非注入调试 socket连接

以上方法都才用了注入dll的方式来进行调试
根据别的老师及官方文档介绍,也有非注入式,采用socket连接的方式来进行调试
由于需要引入socket包,所以暂未在此详细介绍,感兴趣可以参考 https://emmylua.github.io/zh_CN/run/remote.html